//
//  HuManageTools.m
//  need
//
//  Created by AcHu on 15/11/12.
//  Copyright (c) 2015年 oc. All rights reserved.
//

#import "HuManageTools.h"

#define FileFolder @"MYFILE" //存储文件的文件夹名
#define DOUCMENTS_PATH NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject

@implementation HuManageTools

#pragma mark 存储文件data类型
+(BOOL)fileExistsFileName:(NSString *)file{
    NSFileManager *manger = [NSFileManager defaultManager];
    if([manger fileExistsAtPath:[NSString stringWithFormat:@"%@/%@/%@.archiver",DOUCMENTS_PATH,FileFolder,file]]){
        return YES;
    }else{
        return NO;
    }
}

//存
+ (void)fileSetFileName:(NSString *)name type:(id)type{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSDictionary *dic = @{name:type};
    NSString *path = [NSString stringWithFormat:@"%@/%@",DOUCMENTS_PATH,FileFolder];
    NSLog(@"%@",path);
    if (![manger fileExistsAtPath:path]) {
        [manger createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
        NSLog(@"创建文件夹成功");
    }else{
        NSLog(@"文件夹已存在，无须重复创建");
    }
    
    BOOL tag = [NSKeyedArchiver archiveRootObject:dic toFile:[NSString stringWithFormat:@"%@/%@.archiver",path,name]];
    NSLog(@"%@",[NSString stringWithFormat:@"%@/%@.archiver",path,name]);
    if (tag) {
        NSLog(@"文件存储成功");
    }else{
        NSLog(@"文件存储失败");
    }
}

//读
+ (id)fileGetFileName:(NSString *)name{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/%@/%@.archiver",DOUCMENTS_PATH,FileFolder,name];
    if ([manger fileExistsAtPath:path]) {
        NSDictionary *dic = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSLog(@"文件读取成功");
        return dic[name];
    }else{
        NSLog(@"文件不存在");
        return nil;
    }
}

//删
+ (void)fileRemoveByName:(NSString *)name{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/%@/%@.archiver",DOUCMENTS_PATH,FileFolder,name];
    if ([manger fileExistsAtPath:path]) {
        [manger removeItemAtPath:path error:nil];
        NSLog(@"删除单个文件成功");
    }else{
        NSLog(@"文件不存在");
    }
}

//删全部
+ (void)fileRemoveAllData{
    NSFileManager *manger = [NSFileManager defaultManager];
    NSString *path = [NSString stringWithFormat:@"%@/%@",DOUCMENTS_PATH,FileFolder];
    if ([manger fileExistsAtPath:path]) {
        [manger removeItemAtPath:path error:nil];
        NSLog(@"删除全部文件成功");
    }else{
        NSLog(@"文件夹不存在");
    }
}
@end
